﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.LiveFX.ResourceModel;
using System.Windows.Forms;
using Microsoft.LiveFX.Client;
using System.IO;

namespace LiveFrameworkExplorer.Plugins.FileBrowser
{
    public class FileDownloadingPlugin : IPlugin, IPluginItem
    {
        private ILFEBase _lfeBase;

        #region IPlugin Members

        public void Init(ILFEBase lfeBase)
        {
            _lfeBase = lfeBase;
        }

        public List<Type> GetTypes()
        {
            return new List<Type>() { 
                typeof(DataEntryResource)
            };
        }

        public string GetMenuItemLabel()
        {
            return "Download this file";
        }

        public System.Drawing.Image GetMenuItemImage()
        {
            return LiveFrameworkExplorer.Properties.Resources.download;
        }

        public bool CheckItem<TMeshItem, TResource>(object item)
            where TMeshItem : Microsoft.LiveFX.Client.LiveItem<TResource>, new()
            where TResource : Microsoft.LiveFX.ResourceModel.Resource, new()
        {
            var resource = ((item as TMeshItem).Resource) as DataEntryResource;
            if (resource != null)
                return resource.Type == "File" || resource.Type == "MediaResourceEntry";
            else
                return false;
        }

        public void OpenPlugin<TMeshItem, TResource>(object item, object parentItem)
            where TMeshItem : Microsoft.LiveFX.Client.LiveItem<TResource>, new()
            where TResource : Microsoft.LiveFX.ResourceModel.Resource, new()
        {
            var resource = ((item as TMeshItem).Resource) as DataEntryResource;
            SaveFileDialog sfg = new SaveFileDialog();
            sfg.Title = "Save as " + resource.Title;
            sfg.FileName = resource.Title;
            if (sfg.ShowDialog() == DialogResult.OK)
            { 
                var stream = File.Create(sfg.FileName);
                (item as DataEntry).ReadMediaResourceCompleted += new EventHandler<MediaResourceAsyncEventArgs>(FileDownloadingPlugin_ReadMediaResourceCompleted);
                (item as DataEntry).ReadMediaResourceAsync(stream, resource.Title);
                _lfeBase.GetDTEEnvironment().StatusBar.Progress(true, "Starting download of " + resource.Title, 1, 5);
            }
        }

        void FileDownloadingPlugin_ReadMediaResourceCompleted(object sender, MediaResourceAsyncEventArgs e)
        {
            if (e.Error == null)
            {
                e.Result.Flush();
                e.Result.Close();
                _lfeBase.GetDTEEnvironment().StatusBar.Progress(false, "Download finnished !", 5, 5);
                MessageBox.Show(e.UserState as String + " was successfull downloaded !", "Live Framework Explorer",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                _lfeBase.GetDTEEnvironment().StatusBar.Text = "Error to download " + e.UserState as String;
                _lfeBase.WriteToLFEOutput("Error to download " + e.UserState as String + " : " + e.Error.Message);
            }
        }

        #endregion
    }
}
